package com.googlecode.restitory.gae.hook;

import com.googlecode.restitory.gae.AuthKey;
import com.googlecode.restitory.gae.Request;
import com.googlecode.restitory.gae.Response;

public class AccessHook extends AbstractHook {

	@Override
	public Response handle(Request request) {
		if (getAdapter().exists("/_/prepare")) {
			return check(request);
		}
		return getChain().execute(request);
	}

	private Response check(Request request) {
		AuthKey key = AuthHook.getCookie(request);
		if(key == null) {
			return AuthHook.unauthorize(request);
		}
		System.out.println("check " + request + " " + key);
		return getChain().execute(request);
	}

}
